from datetime import datetime
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from tagging.fields import TagField
import clapton.utils.publisher as publisher

class Poll(models.Model):
    site = models.ForeignKey(Site, verbose_name=_(u'Site'),) 
    user = models.ForeignKey(User, verbose_name=_(u'User'),)
    title = models.CharField(_(u'Title'), max_length=200,)
    slug = models.SlugField(_(u'Slug'), 
                            help_text=_(u'Automatically built from the title.'),)
    pub_date = models.DateTimeField(_(u'Date published'), 
	                            default=datetime.now, 
				    help_text=_(u'Publication date of the poll.'),)
    tags = TagField(verbose_name=_(u'Tags'), blank=True, null=True, 
	            help_text=_(u'Separate tags with spaces or commas.'))
    publication_status = models.CharField(_(u'Publication Status'), 
	                                  max_length=32, choices=publisher.PUBLICATION_CHOICES,
					  default='Published',
					  help_text=_(u'Only published entries will appear on the site. Drafts will only be visible on the Administration page.'),)
    
    objects = models.Manager()
    published_objects = publisher.PublishedManager()
    
    class Meta:
        verbose_name_plural = _(u'polls')
        ordering = ['-pub_date']
        get_latest_by = 'pub_date'

    def __unicode__(self):
    	return self.title

    def get_absolute_url(self):
        return '/polls/%s/%s/' % (self.pub_date.strftime("%Y/%b/%d").lower(),
                                  self.slug)

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(_(u'Choice'), max_length=80)
    votes = models.IntegerField(_(u'Votes'), default='0',)

    def __unicode__(self):
    	return self.choice
